package cn.b.sky.mes.mm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.mm.dao.PFEPDao;
import cn.b.sky.mes.mm.model.PFEP;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.condition.PFEPCondition;
import cn.b.sky.mes.mm.service.PFEPService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * 
 * <strong>Title : PFEPServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.service.impl<br></strong>
 * <strong>Description : </strong>PFEP业务实现<br> 
 * <strong>Create on : 2016年9月10日 下午4:54:37<br></strong>
 * <p>
 * @author zhaoxin01<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("pFEPService")
public class PFEPServiceImpl implements PFEPService{
  
  private static final Logger LOG = LoggerFactory.getLogger(PFEPServiceImpl.class);
  
  @Resource
  PFEPDao pFEPDao;
  
  @Override
  public List<PFEP> selectPFEPByCondition(PFEPCondition pFEPCondition) {
    if(pFEPCondition==null){
      return null;
    }
    return pFEPDao.selectPFEPByCondition(pFEPCondition);
  }

  @Override
  public int selectPFEPCount(PFEPCondition pFEPCondition) {
    if(pFEPCondition==null){
      return 0;
    }
    return pFEPDao.selectPFEPCount(pFEPCondition);
  }

  @Override
  public PFEP selectPFEP(String siteCode, String materialCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(materialCode)) {
      return null;
    }
    return pFEPDao.selectPFEP(siteCode, materialCode);
  }

  @Override
  public void insertPFEP(PFEP pFEP) {
    if(pFEP==null){
      return;
    }
    pFEPDao.insertPFEP(pFEP);
  }

  @Override
  public void updatePFEP(PFEP pFEP) {
    if(pFEP==null){
      return;
    }
    pFEPDao.updatePFEP(pFEP);
  }

  @Override
  public BatchJobReport deletePFEP(String siteCode, String materialCode) {
    if (StringUtils.isEmpty(siteCode) || StringUtils.isEmpty(materialCode)) {
      return new BatchJobReport("", "siteCode or materialCode is null", false);
    }
    String message = materialCode;
    boolean success = true;
    String cause = "";
    try {
      pFEPDao.deletePFEP(siteCode, materialCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success);
  }

}
